//
// Created by czm on 2023/5/15.
//
#include "esp_log.h"
#include "RgbLed.h"

#define TAG "RgbLed"

RgbLed::RgbLed() : Device("") {
    m_strip.type = LED_TYPE;
    m_strip.length = LED_NUM;
    m_strip.gpio = LED_DATA_GPIO;
    m_strip.buf = nullptr;
    m_strip.brightness = 255;
    m_strip.channel = RMT_CHANNEL_1;

    led_strip_install();
    esp_err_t ret = led_strip_init(&m_strip);
    if (ret != ESP_OK) {
        ESP_LOGE(TAG, "Error init RgbLed...");
    }
    setColor(0,0,0);
}

RgbLed::~RgbLed() = default;

void RgbLed::setColor(uint8_t r, uint8_t g, uint8_t b) {
    int64_t now = esp_timer_get_time();
    if (r == m_color.r && g == m_color.g && b == m_color.b) {
        // if (now - m_last_update_time <= 200000) { //1s内不更新
        //     return;
        // }
        return;
    }
    rgb_t rgb = {.r = r, .g=g, .b=b};
    led_strip_fill(&m_strip, 0, m_strip.length, rgb);
    led_strip_flush(&m_strip);
    m_color.r = r;
    m_color.g = g;
    m_color.b = b;
    m_last_update_time = now;
//   ESP_LOGI(TAG,"RgbLed color=%d,%d,%d",r,g,b);
}

const rgb_t &RgbLed::getColor() const {
    return m_color;
}

void RgbLed::process() {
    Device::process();
//    ESP_LOGI(TAG,"RgbLed::process");
//    setColor(g_hold_info.led_r,g_hold_info.led_g,g_hold_info.led_b);
}
